home *** CD-ROM | disk | FTP | other *** search
- /*
- * SFcolours - Star Fighter 3000 colours editor
- * Menu attached to colours window (all levels)
- * Copyright (C) 2001 Chris Bazley
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public Licence as published by
- * the Free Software Foundation; either version 2 of the Licence, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public Licence for more details.
- *
- * You should have received a copy of the GNU General Public Licence
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
-
- /* ANSI library files */
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
-
- /* RISC OS library files */
- #include "kernel.h"
- #include "wimp.h"
- #include "toolbox.h"
- #include "event.h"
- #include "wimplib.h"
- #include "menu.h"
-
- /* My library files */
- #include "err.h"
- #include "msgtrans.h"
- #include "Macros.h"
- #include "Pal256.h"
- #include "FilePerc.h"
-
- /* Local headers */
- #include "EditColmap.h"
- #include "SFCFileInfo.h"
- #include "SFCSaveBox.h"
- #include "Utils.h"
- #include "DCS_dialogue.h"
- #include "Main.h"
- #include "Menus.h"
-
- /* Menu entries */
- #define FILEMENU_INFO 0x01
- #define FILEMENU_SAVE 0x02
- #define FILEMENU_DISCARD 0x00
-
- #define EDITMENU_SETCOL 0x01
- #define EDITMENU_SMOOTH 0x04
- #define EDITMENU_SELECTALL 0x05
- #define EDITMENU_CLEARSEL 0x03
-
- ObjectId ColsMenu_sharedid = NULL_ObjectId, EditMenu_sharedid = NULL_ObjectId;
-
- /* ----------------------------------------------------------------------- */
- /* Function prototypes */
-
- static ToolboxEventHandler _EditMenu_showhandler, _FileMenu_selectionhandler, _EditMenu_colourselhandler, _EditMenu_selectionhandler;
- static void _EditMenu_fade(ViewData *view_data);
-
- /* ----------------------------------------------------------------------- */
- /* Public functions */
-
- void ColsMenu_initialise(ObjectId id)
- {
- ColsMenu_sharedid = id;
- }
-
- /* ----------------------------------------------------------------------- */
-
- void FileMenu_initialise(ObjectId id)
- {
- /* Install handlers */
- EF(event_register_toolbox_handler(id, Menu_Selection, _FileMenu_selectionhandler, NULL));
- }
-
- /* ----------------------------------------------------------------------- */
-
- void EditMenu_initialise(ObjectId id)
- {
- EditMenu_sharedid = id;
-
- /* Install handlers */
- EF(event_register_toolbox_handler(id, Menu_AboutToBeShown, _EditMenu_showhandler, NULL));
- EF(event_register_toolbox_handler(id, Menu_Selection, _EditMenu_selectionhandler, NULL));
- EF(event_register_toolbox_handler(pal256_sharedid, Pal256_ColourSelected, _EditMenu_colourselhandler, NULL));
- }
-
- /* ----------------------------------------------------------------------- */
- /* Private functions */
-
- static int _FileMenu_selectionhandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
- {
- /* Info to be extracted from ancestor Colour map window: */
- ViewData *view_data;
- E_RETV(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data), 0)
-
- switch(id_block->self_component) {
- case FILEMENU_INFO:
- RE(toolbox_show_object(Toolbox_ShowObject_AsMenu, fileinfo_sharedid, Toolbox_ShowObject_AtPointer, NULL, id_block->self_id, id_block->self_component))
- return 1; /* claim event */
-
- case FILEMENU_SAVE:
- if(strchr(view_data->last_savepath, (int)'.') == NULL) {
- /* Must open savebox */
- RE(toolbox_show_object(Toolbox_ShowObject_AsMenu, savebox_sharedid, Toolbox_ShowObject_AtPointer, NULL, id_block->self_id, id_block->self_component))
- }
- else {
- /* Save file immediately */
- _kernel_oserror *err = perc_operation(FILEPERC_OP_COMP, view_data->last_savepath, FILETYPE_FEDNET, (flex_ptr)&view_data->colour_map);
- if(err != NULL)
- /* Saving error */
- err_report(err->errnum, msgs_lookup_sub1("SaveFail", err->errmess));
- else
- EditColmap_newfile(view_data, view_data->last_savepath, 1);
- }
- return 1; /* claim event */
-
- case FILEMENU_DISCARD:
- if(view_data->changed_since_save) {
- /* Unsaved data - prompt user for action */
- dcs_openparent = false;
- RE(open_topleftofwin(Toolbox_ShowObject_AsMenu, dcs_sharedid, id_block->ancestor_id, id_block->self_id, id_block->self_component))
- }
- else {
- /* Close immediately */
- RE(toolbox_delete_object(0, id_block->ancestor_id))
- }
- return 1; /* claim event */
- }
-
- return 0; /* event not handled */
- }
-
- /* ----------------------------------------------------------------------- */
-
- static int _EditMenu_colourselhandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
- {
- ViewData *view_data;
-
- if(id_block->parent_id != EditMenu_sharedid)
- return 0; /* none of our business */
-
- E_RETV(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data), 1)
- EditColmap_set_selcolour(view_data, ((Pal256ColourSelectedEvent *)event)->colour_number);
-
- return 1; /* claim event */
- }
-
- /* ----------------------------------------------------------------------- */
-
- static int _EditMenu_selectionhandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
- {
- ViewData *view_data;
- E_RETV(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data), 0)
-
- switch(id_block->self_component) {
- case EDITMENU_SETCOL:{
- int sel_colour = EditColmap_get_selcolour(view_data);
- if(sel_colour != -1) {
- if(!E(Pal256_set_colour(pal256_sharedid, (char)sel_colour)))
- RE(toolbox_show_object(Toolbox_ShowObject_AsMenu, pal256_sharedid, Toolbox_ShowObject_AtPointer, NULL, id_block->self_id, id_block->self_component))
- }
- else
- _kernel_oswrch(7); /* beep */
- }return 1; /* claim event */
-
- case EDITMENU_SMOOTH:
- EditColmap_smoothselection(view_data); /* checks internally for insufficient selection */
- return 1; /* claim event */
-
- case EDITMENU_SELECTALL:
- EditColmap_selectall(view_data);
- _EditMenu_fade(view_data);
- return 1; /* claim event */
-
- case EDITMENU_CLEARSEL:
- EditColmap_clearselection(view_data);
- _EditMenu_fade(view_data);
- return 1; /* claim event */
- }
-
- return 0; /* event not handled */
- }
-
- /* ----------------------------------------------------------------------- */
-
- static int _EditMenu_showhandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
- {
- /* Is there a selection? */
- ViewData *view_data;
-
- if(!E(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data)))
- _EditMenu_fade(view_data);
-
- return 1; /* claim event */
- }
-
- /* ----------------------------------------------------------------------- */
-
- static void _EditMenu_fade(ViewData *view_data)
- {
- /* Determine whether none/some/all are selected */
- int num_selected = 0;
- int all_sel = 1;
- for(int entry = 0; entry < view_data->num_cols; entry++) {
- if(view_data->sel_table[entry])
- num_selected++; /* increment count of selected */
- else
- all_sel = 0; /* none are selected */
- }
-
- /* If no selection exists prevent setting actual colour */
- RE(menu_set_fade(0, EditMenu_sharedid, EDITMENU_SETCOL, num_selected < 1))
-
- /* If less than 3 logical colours selected then prevent interpolation */
- RE(menu_set_fade(0, EditMenu_sharedid, EDITMENU_SMOOTH, num_selected < 3))
-
- /* If full selection then prevent select all */
- RE(menu_set_fade(0, EditMenu_sharedid, EDITMENU_SELECTALL, all_sel))
-
- /* If no selection then prevent clear selection */
- RE(menu_set_fade(0, EditMenu_sharedid, EDITMENU_CLEARSEL, num_selected < 1))
- }
-